<?php
namespace api\controllers;

use Yii;
use filsh\yii2\oauth2server\filters\auth\CompositeAuth;
use api\components\TransmitException;
use yii\filters\auth\HttpBearerAuth;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use common\models\Certificate;


/**
 * 认证控制器
 * @author ZCY
 */
class CertificateController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class'       => CompositeAuth::className(),
                'authMethods' => [
                    ['class' => HttpBearerAuth::className()],
                ],
                'optional'    => ['*'],
            ],
        ]);
    }

 

    /**
     * 获取认证token
     */
    public function actionGetToken()
    {
        $params = Yii::$app->request->post();
        $model = new Certificate();
        $model->setScenario(Certificate::SCENARIO_GETTOKEN);
        if ($model->load(['Certificate' => $params])) {
            if ($result = $model->getToken()) {
                return ['message' => '获取token信息成功', 'data' => $result];
            }
        }
    }
    
    /**
     * 上传认证证件
     */
    public function actionCertificate(){
        $params = Yii::$app->request->post();
        $model = new Certificate();
        $model->setScenario(Certificate::SCENARIO_CERTIFICATE);
        if ($model->load(['Certificate' => $params])) {
            if ($result = $model->certificate()) {
                return ['message' => '获取认证信息成功', 'data' => $result];
            }
        }
    }
    
    /**
     * 证件类型接口
     */
    public function actionCardTypes(){  
        $cardTypes=Yii::$app->params['cardType'];
        $cardItems=[];
        foreach($cardTypes as $key=>$value){
            $cardItem['id']=$key;
            $cardItem['name']=$value;
            $cardItems[]=$cardItem;
        } 
        return ['message' => '证件类型信息获取成功', 'data' => $cardItems];                   
    }

}
